package com.gidgabs.common.utils;




import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.time.LocalDate;
import java.util.Base64;
import java.util.Objects;

/**
 * Created by xiaob on 2017/2/27.
 */
public class LangUtil {
    /**上一秒*/
    private static int preSecd=0;
    /**递增值*/
    private static int seq=0;
    /**
     * 根据日期生成一个路径
     * @return
     */
    public static String createPathByDate(){
        LocalDate localDate=LocalDate.now();
        int year = localDate.getYear();
        int month = localDate.getMonth().getValue() + 1;
        int dayOfMonth = localDate.getDayOfMonth();
        return year+"/"+month+"/"+dayOfMonth;
    }

    /**
     * 获取web项目的父路径,即为webapps的路径
     * @return
     */
    public static String webappsPath(){
        String path = LangUtil.class.getClassLoader().getResource("").getPath();
        for (int i=0;i<4;i++){//去除4个/,即为前3层
            path=path.substring(0,path.lastIndexOf("/"));
        }
        return path;
    }

    /**
     * mysql查询分页时,算出应该从第几行开始获取数据
     * @param pageSize
     * @param pageNo
     * @return
     */
    public static Integer firstLimit(Integer pageSize,Integer pageNo){
        if (pageSize==null)  pageSize=10;
        if (pageNo==null)  pageNo=1;
        return (pageNo-1)*pageSize;
    }


    /**
     *
     * @param num 被格式化的字符串
     * @param len 小数位数
     * @return
     */
    public static String formatNum(String num,int len){
        Objects.requireNonNull(num);
        int var=0;
        String endStr="";
        int index = num.indexOf(".");
        if (index <0){
            for (int i=0;i<len;i++){
                endStr+="0";
            }
            return num+"."+endStr;
        }
        int cha = num.length() - 1-index - len;
        if (cha>0){//此时去掉末尾多余的位数
            return num.substring(0,num.length()-cha);
        }else {//此时应该在末尾添加零
            for (int i=0;i>cha;i--){
                endStr+="0";
            }
            return num+endStr;
        }
    }

    public static String getMD5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            return new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            e.printStackTrace();
            return str;
        }
    }

    /**
     * 生成指定位数的随机数
     * @param len
     * @return
     */
    public static String generateCode(int len) {
        int randomNum = (int)(Math.random() * 9000) + 1000;
        return String.valueOf(randomNum);
    }

    public static String encodeBase64(String string){
        return  Base64.getEncoder().encodeToString(string.getBytes(Charset.forName("utf-8")));
    }

    public static String decodeBase64(String base64Str){
        return new String(Base64.getDecoder().decode(base64Str), Charset.forName("utf-8"));
    }


    /**
     * 获取唯一字符串
     * @return
     */
    public synchronized static String uniqueCode(){
        int currentSed = (int) (System.currentTimeMillis() / 1000);
        if (currentSed==preSecd){
            return to52Jinzhi(preSecd+++seq);
        }else {
            preSecd=currentSed;
            seq=0;
            return to52Jinzhi(preSecd);
        }

    }
    private static String to52Jinzhi(int data) {
        String str = "ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        int scale = str.length(); //转化目标进制
        String s = "";
        if(data==0)
        {
            return str.charAt(0)+"";
        }
        while(data > 0){
            if(data < scale){
                s = str.charAt(data) + s;
                data = 0;
            }else{
                int r = data%scale;
                s = str.charAt(r) + s;
                data  = (data-r)/scale;
            }
        }
        return s;
    }


}
